$(function() {
	start();
	$(window).on('scroll', function() {
		start();
	})
	var carPicSwi = new Swiper('.carPicSwi .swiper-container', {
		pagination: '.carPicSwi .swiper-pagination',
		paginationType: 'fraction',
		effect: 'effect'
	});
	var swiperBanner = new Swiper('.indexBanner .swiper-container', {
		pagination: '.indexBanner .swiper-pagination',
		autoplay: 4000,
		paginationClickable: true
	});
	$("#collect").click(function() {
		var aid = $(this).attr("aid");
		var sid = $(this).attr("sid");
		$.ajax({
			url: "/article_collect",
			type: "post",
			data: {"id":aid,"sid":sid},
			dataType: "json",
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
					if(sid == 1){
						$("#collect").removeClass("active");
						$("#collect").attr("sid","0");
					}else{
						$("#collect").addClass("active");
						$("#collect").attr("sid","1");
					}
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		})
	});
	/*===成为会员--发送验证码===*/
	var countdown = 60;
	$(".member #yzmBtn").click(function() {
		var mobile = $("#mobile").val();
		if ($(this).hasClass("disabled")) {
			return false;
		}
		if (!mobile) {
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if (!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		settime($(this), countdown);
		//后台发送验证码
		$.ajax({
			url: '/phone',
			data: {
				"phoneNum":mobile
			},
			type: "post",
			dataType: "json",
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	/*===成为会员--提交表单===*/
	$("#memberForm").click(function() {
		var username = $("#username").val();
		var mobile = $("#mobile").val();
		var yzm = $("#yzm").val();
		if (!username) {
			layer.msg("姓名不能为空!");
			return false;
		}
		if (!mobile) {
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if (!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		if (!yzm) {
			layer.msg("验证码不能为空!");
			return false;
		}
		$.ajax({
			url: '/binding',
			data: {
				"phoneNum":mobile,
				"username":username,
				"yzm":yzm,
			},
			type: "post",
			dataType: "json",
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
					setTimeout(function() {
						location.href = data.url;
					}, 1000);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	/*===复制链接===*/
	$("#shareLink").click(function() {
		copyText($("#copyText").text());
	});
	/*===绑定手机号--发送验证码===*/
	var countdown2 = 60;
	$(".mobile #yzmBtn").click(function() {
		var mobile = $("#mobile").val();
		if ($(this).hasClass("disabled")) {
			return false;
		}
		if (!mobile) {
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if (!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		settime($(this), countdown2);
		//后台发送验证码
		$.ajax({
			url: '/phone',
			data: {
				"phoneNum":mobile
			},
			type: "post",
			dataType: "json",
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	/*===绑定手机号--提交表单===*/
	$("#mobileForm").click(function() {
		var mobile = $("#mobile").val();
		var yzm = $("#yzm").val();
		if (!mobile) {
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if (!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		if (!yzm) {
			layer.msg("验证码不能为空!");
			return false;
		}
		$.ajax({
			url: '/mobile',
			data: {
				"phoneNum":mobile,
				"yzm":yzm,
			},
			type: "post",
			dataType: "json",
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
					setTimeout(function() {
						location.href = data.url;
					}, 1000);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	/*===购车报单--提交表单===*/
	$("#buycarForm").click(function() {
		var username = $("#username").val();
		var mobile = $("#mobile").val();
		var type = $("#type").val();
		if (!username) {
			layer.msg("购车人姓名不能为空!");
			return false;
		}
		if (!mobile) {
			layer.msg("手机号不能为空!");
			return false;
		}
		var reg = /(1[3-9]\d{9}$)/;
		if (!reg.test(mobile)) {
			layer.msg("手机号格式不正确!");
			return false;
		}
		if (!type) {
			layer.msg("购买车型不能为空!");
			return false;
		}
		$.ajax({
			url: '/form_add',
			data: {
				"name":username,
				"tel":mobile,
				"vehicle_type":type
			},
			type:'post',
			dataType:'json',
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
					setTimeout(function() {
						location.href = data.url;
					}, 1000);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	// 进度条
	$("#processbar").css("width", parseFloat($("#processbar").attr("data-percent")) * 100 + '%')
	/*===积分兑换--提交表单===*/
	$("#tixianBtn").click(function() {
		var cardUser = $("#cardUser").val();
		var cardNum = $("#cardNum").val();
		var cardName = $("#cardName").val();

		var tixianMoney = $("#tixianMoney").val();
		var yue = parseFloat($("#yue").text());

		if (!cardUser) {
			layer.msg("持卡人姓名不能为空!");
			return false;
		}
		if (!cardNum) {
			layer.msg("持卡人卡号不能为空!");
			return false;
		}
		if (!cardName) {
			layer.msg("开户行不能为空!");
			return false;
		}
		if(tixianMoney>yue){
			layer.msg("提现数量不能大于剩余积分数量！");
			return false;
		}
		if(tixianMoney<1000){
			layer.msg("提现数量必须大于1000积分！");
			return false;
		}
		if((tixianMoney%1000)!=0){
			layer.msg("提现数量必须为1000的整数倍！");
			return false;
		}
		$.ajax({
			url: '/extract',
			data: {
				"cardUser":cardUser,
				"cardNum":cardNum,
				"cardName":cardName,
				"tixianMoney" : tixianMoney,
				"yue" : yue
			},
			type:'post',
			dataType:'json',
			success: function(data) {
				if(data.code == '1') {
					layer.msg(data.msg);
					setTimeout(function() {
						location.href = data.url;
					}, 1000);
				} else {
					layer.msg(data.msg);
				}
			},
			error: function() {
				layer.msg("网络连接错误!");
			}
		});
	});
	//tabchange
	$(".tabBars .tabBar").eq(0).addClass("active");
	$(".tabCons .tabCon").eq(0).addClass("active");
	tabChange(".tabBars .tabBar", ".tabCons .tabCon", "active");
})
//方法
//tab切换
function tabChange(tab, con, ac) {
	$(tab).click(function() {
		var i = $(this).index();
		$(this).addClass(ac).siblings().removeClass(ac);
		$(con).eq(i).addClass(ac).siblings().removeClass(ac);
	});
}
//topback

//禁止滚动条和滚轮滚动
function unScroll() {
	var top = $(document).scrollTop();
	$(document).on('scroll.unable', function(e) {
		$(document).scrollTop(top);
	});
	$(document).bind('mousewheel', function(event, delta) {
		return false;
	});
}
//移除禁止滚动条滚动
function removeUnScroll() {
	$(document).unbind("scroll.unable");
	$(document).unbind('mousewheel');
}
//复制链接
function copyLink(event) {
	const range = document.createRange();
	range.selectNode(document.getElementById('link')); //被复制的文本
	const selection = window.getSelection();
	if (selection.rangeCount > 0) selection.removeAllRanges();
	selection.addRange(range);
	document.execCommand('copy');
}
//lazyLoad

function start() {
	//.not('[data-isLoaded]')选中已加载的图片不需要重新加载
	$('img').not('[data-isLoaded]').each(function() {
		var $node = $(this)
		if (isShow($node)) {
			loadImg($node)
		}
	})
}
//判断一个元素是不是出现在窗口(视野)
function isShow($node) {
	return $node.offset().top <= $(window).height() + $(window).scrollTop()
}
//加载图片
function loadImg($img) {
	//.attr(值)
	//.attr(属性名称,值)
	$img.attr('src', $img.attr('data-src')) //把data-src的值 赋值给src
	$img.attr('data-isLoaded', 1) //已加载的图片做标记
}

/*===发送验证码倒计时===*/
function settime(obj, time) {
	var countdown = time;
	if (countdown == 0) {
		obj.removeClass('disabled');
		obj.text("获取验证码");
		countdown = 60;
		return;
	} else {
		obj.addClass('disabled');
		obj.text("重新发送(" + countdown + ")");
		countdown--;
	}
	setTimeout(function() {
		settime(obj, countdown)
	}, 1000)
}
// 复制文本内容到剪切板
function copyText(obj) {
	if (!obj) {
		return false;
	}
	var text;
	if (typeof(obj) == 'object') {
		if (obj.nodeType) { // DOM node
			obj = $(obj); // to jQuery object
		}
		try {
			text = obj.text();
			if (!text) { // Maybe <textarea />
				text = obj.val();
			}
		} catch (err) { // as JSON
			text = JSON.stringify(obj);
		}
	} else {
		text = obj;
	}
	//var $temp = $('<input>'); // Line feed is not supported
	var $temp = $('<textarea>');
	$('body').append($temp);
	$temp.val(text).select();
	var res = document.execCommand('copy');
	$temp.remove();
	layer.msg("复制成功!");
	return res;
}

function checkNum(obj) {
	obj.value = obj.value.replace(/[^\d.]/g, ""); //是否是数字 和小数点，若是除数字 和小数点之外的则替换为空
	obj.value = obj.value.replace(/^\./g, ""); //确证第一个为数字而不是“.”
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只能输入一个“.”
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //保证”.“只出现一次，而不能出现两次以上
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数   
}
